import $api from "@/api/index.js"
import $util from "@/utils/index.js"
import $store from "@/store/index.js"
export default {
	state: {
		pageActive: false,
		activeIndex: 0,
		tabList: [{
			title: '全部',
			sort: 'top desc',
		}, {
			title: '价格',
			sort: 'price',
			sign: 0,
			is_sign: 1,
		}, {
			title: '销量',
			sort: 'total_sale',
			sign: 0,
			is_sign: 1,
		}, {
			title: '好评度',
			sort: 'star',
			sign: 0,
			is_sign: 1,
		}],
		param: {
			page: 1,
			sort: ''
		},
		list: {
			data: [],
			last_page: 1,
			current_page: 1
		},
		banner: [],
		service_cate: [],
		service_all_cate: [],
		recommend_list: [],
		recommend_style: 1
	},
	mutations: {
		async updateServiceItem(state, item) {
			let {
				key,
				val
			} = item
			state[key] = val
		}
	},
	actions: {
		async getServiceIndex({
			commit,
			state
		}, param) {
			let d = await $api.service.index(param);
			let {
				banner = [],
					service_cate = [],
					recommend_list = [],
					recommend_style = 1,
					coach_apply_show = 0
			} = d
			let cate = $util.deepCopy(service_cate)
			let cateLen = service_cate.length
			if (coach_apply_show) {
				cate.splice(cateLen > 4 ? 4 : cateLen, 0, {
					id: -1,
					title: $store.state.config.configInfo.attendant_name + '入驻',
					cover: 'https://lbqny.migugu.com/admin/anmo/technician/default_technician.png',
					url: '/technician/pages/apply?type=1'
				})
			}
			let num = ((coach_apply_show && cateLen > 28) || (!coach_apply_show && cateLen > 29)) ? 29 :
				coach_apply_show ? cateLen + 1 : cateLen
			cate = cate.slice(0, num)
			if ((cateLen > (coach_apply_show ? 28 : 29))) {
				cate.push({
					id: 0,
					title: '全部分类',
					cover: '',
					icon: 'iconfenleiyingyongtongzhi2',
					url: '/user/pages/service/cate'
				})
			}
			let cateInd = service_cate.findIndex(item => {
				return item.id == 0
			})
			if (cateInd !== -1) {
				service_cate.splice(cateInd, 1)
			}
			commit('updateServiceItem', {
				key: 'banner',
				val: banner
			})
			commit('updateServiceItem', {
				key: 'service_cate',
				val: cate
			})
			commit('updateServiceItem', {
				key: 'service_all_cate',
				val: service_cate
			})
			commit('updateServiceItem', {
				key: 'recommend_list',
				val: recommend_list
			})
			commit('updateServiceItem', {
				key: 'recommend_style',
				val: recommend_style
			})
		},
		async getServiceList({
			commit,
			state
		}, param) {
			let {
				realtime_location = 0
			} = $store.state.config.configInfo
			if (!realtime_location) {
				delete param.lat
				delete param.lng
			}
			let methodModel = realtime_location ? 'getCoachService' : 'serviceList'
			let d = await $api.service[methodModel](param)
			let oldList = state.list;
			let newList = d;
			let list = {}
			if (param.page == 1) {
				list = newList;
			} else {
				newList.data = oldList.data.concat(newList.data)
				list = newList;
			}
			commit('updateServiceItem', {
				key: 'list',
				val: list
			})
		}
	},
}